<?php 
include('includes/connexion.php');
$adminmanager = new AdminManager($connexion);
$elevemanager = new EleveManager($connexion);
session_start();
include('includes/secu.php');
if(!isset($_SESSION['Admin']))
	header('Location: login.php');
?>
<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
  <title>Tables Advanced - Target Admin</title>

  <meta charset="utf-8">
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width">

  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,700italic,400,600,700">
  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald:400,300,700">
  <link rel="stylesheet" href="./css/font-awesome.min.css">
  <link rel="stylesheet" href="./js/libs/css/ui-lightness/jquery-ui-1.9.2.custom.min.css">
  <link rel="stylesheet" href="./css/bootstrap.min.css">

  <!-- Plugin CSS -->
  <link rel="stylesheet" href="./js/plugins/icheck/skins/minimal/blue.css">
  <link rel="stylesheet" href="js/plugins/select2/select2.css">
  <link rel="stylesheet" href="js/plugins/datepicker/datepicker.css">
  <link rel="stylesheet" href="js/plugins/simplecolorpicker/jquery.simplecolorpicker.css">
  <link rel="stylesheet" href="js/plugins/timepicker/bootstrap-timepicker.css">
  <link rel="stylesheet" href="js/plugins/fileupload/bootstrap-fileupload.css">
  <link rel="stylesheet" href="js/libs/css/ui-lightness/jquery-ui-1.9.2.custom.css">
  <link rel="stylesheet" href="js/plugins/magnific/magnific-popup.css">
  <!-- App CSS -->
  <link rel="stylesheet" href="./css/target-admin.css">
  <link rel="stylesheet" href="./css/custom.css">
  <!-- Page CSS -->
  <link rel="stylesheet" href="css/demos/ui-notifications.css">

  <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
  <![endif]-->
</head>
<?php 

require_once('includes/connexion.php');
	$reponse = new ReponseManager($connexion);// on instancie une réponse avec la connexion en parametre
	$maQuestion= new QuestionManager($connexion); //  on instancie une question pour récupérer le nom de la question
	$monExercice = new ExerciceManager($connexion); // on instancie un exercice manager
	$maPosition = new ReponseTxtTrousPositionManager($connexion); // on instancie la classe de texte à trous
	if(!empty($_GET['idQuestion'])){
		$idQuest = $_GET['idQuestion'];
	}else{
		$idQuest = $_POST['idRefQuestion'];
	}
	
	$tabReponse = $reponse->getByQuestion($idQuest); // on récupere la liste des réponse dans le tableau
	
	
	
	// si le champs del existe on supprimer l'association mot/texte.
	if((!empty($_GET['del'])) && ($_GET['del'] == true) ){
		
		$maPosition->delete($maPosition->get($idQuest));// on selectionne le reponsetxttrous avec l'id de la question et on le delete
		header('Location:reponseTxtTrousPosition.php?idQuestion='.$idQuest); // on redirige
		
	}  	

	
		function test (){ // on test si les id valent 0 ou non
			$tabMotTest = $_POST['mot'];
			foreach($tabMotTest as $test){
				if($test == 0 ){
					return false;
				}
			}
			return true;
		}
		
			
	
    // si les champs sont remplis on ajoute à la bdd         	
	if(isset($_POST['valider']) ){
		if(test() == true){
			$tabMot = $_POST['mot'];
			var_dump ($tabMot);
			foreach($tabMot as $key => $mot){ 
				$monMot = new ReponseTxtTrousPosition(); // on prépare la classe txt trous
				$monMot->setIdQuestion($_POST['idRefQuestion']); // id de la question
				$monMot->setIdReponse($mot); // id du mot
				$monMot->setNumMot($key); // position du mot (de 0 à n)
				$maPosition->add($monMot); // ajout du mot en bdd
			}			
			header('Location:reponseTxtTrousPosition.php?idQuestion='.$idQuest); // on redirige				
		}
	}
	else{}
				
?>
<body>

<?php include('includes/navbar.php'); ?>

<?php include('includes/mainbar.php'); ?>

<div class="container">

  <div class="content">

    <div class="content-container">

      <div class="content-header">
        <h2 class="content-header-title">Réponses</h2>
             <a href="gestion.php">Tests</a>
             / 
             <a href="listexercice.php">Exercices</a> 
             /  
             <a href="question.php?idExercice=<?php echo $maQuestion->get($idQuest)->idRefExercice(); ?>">Questions</a> 
             / 
             <a href="reponseTxtTrous.php?idQuestion=<?php echo $idQuest; ?>">Réponses</a>
             /
             Assignation
      </div> <!-- /.content-header --> 

      <div class="row">
        
        <div class="col-md-12">

          <div class="portlet">

            <div class="portlet-header">

              <h3>
                <i class="fa fa-table"></i>
                Ordre des mots
              </h3>

            </div> <!-- /.portlet-header -->

            <div class="portlet-content">           

              <div class="table-responsive">
				
                <?php 
				
				// affichage du mot final 
				if ($maPosition->getByQuestion($idQuest) != false){ //si on a déjà ajouté les mots
					
					$nbOccurences = substr_count($maQuestion->get($idQuest)->contenu(), '[/mot]'); // nombre de trous dans le texte
					$motComplet = $maQuestion->get($idQuest)->contenu(); // le texte complet avec [/mot]
					$tabMots =  $maPosition->getByQuestion($idQuest); // tableau d'objet de mots
					$tabTxt = explode("[/mot]", $motComplet); // tableau de chaque partie du texte sans les occurences
					
					$SegFinal = $tabTxt[0];
					foreach($tabTxt as $key => $unSegment){
						
						
						//$SegFinal = $SegFinal. " ".$unSegment;
						
						if($key < count($tabTxt)-1){ // si le nb de phrase qui compose le txt est sup à la position on continu
							$SegFinal = $SegFinal." <font color='red'><b>[".$reponse->get($tabMots[$key]->idReponse())->contenu()."]</b></font> ".$tabTxt[$key+1]; // ajout du mot dans la phrase 																																										+ la phrase suivante
						}
						else{}
					}

				}
				else{ // sinon
					$SegFinal = $maQuestion->get($idQuest)->contenu();
				}
					?>
					<div class="row">
						<div class="col-md-12">
							<div class="portlet">
									<div class="portlet-header">
										  <h3>
                                            Votre texte actuel
										  </h3>			
									</div> <!-- /.portlet-header -->
	
									<div class="portlet-content">  
										<div class="table-responsive">
                                        	<?php
                                            echo "<p align='right'>Votre texte contient <b>".substr_count($maQuestion->get($idQuest)->contenu(), '[/mot]')."</b> mots.</p>";
                                            echo $SegFinal; // le mot final	
											?>	
										</div> <!-- /.table-responsive -->
								</div> <!-- /.portlet-content -->
							</div> <!-- /.portlet -->
						</div> <!-- /.col -->
					</div> <!-- /.row -->					
                    <?php
					
					// création du select
					$selectOpen = "<select name='mot[]'>";
					$selectClose = "</select>";
					$selectCorps = "";
					$selectCorps = "<option value=' '> </option>";
					foreach($tabReponse as $rep){ 
						$selectCorps = $selectCorps."<option value='".$rep->id()."'>".$rep->contenu()."</option>";
					}
					$selectFinal = $selectOpen.$selectCorps.$selectClose; // assemblage select
					
					
					$new_mot = str_replace("[/mot]", $selectFinal, $maQuestion->get($idQuest)->contenu()); // on remplace les balises par le select


				?>
      <?php
      		if ($maPosition->getByQuestion($idQuest) == false){ // si on a pas de réponse
	  ?>
      			<div class="row">
        			<div class="col-md-12">
          				<div class="portlet">
             					<div class="portlet-header">
                                      <h3>
                                        Ajouter les mots dans le texte
                                      </h3>			
            					</div> <!-- /.portlet-header -->

            					<div class="portlet-content">  
            						<div class="table-responsive">
                                        <form action="reponseTxtTrousPosition.php" method="post" enctype="multipart/form-data">
                                            <?php
                                                
                                                echo $new_mot;
                                                echo "<br/>";
												
												
                                               		echo "<input type='submit' name ='valider' class='btn btn-success' value='Ajouter une Réponse'/>";
												
												
                                            ?>
                                            <input type="hidden" value ="<?php echo $idQuest; ?>" name="idRefQuestion"/>
                                        </form>
            						</div> <!-- /.table-responsive -->
 							</div> <!-- /.portlet-content -->
            			</div> <!-- /.portlet -->
            		</div> <!-- /.col -->
            	</div> <!-- /.row -->
           <?php } 
		   
		   else{ // sinon
echo '<input type="button" value="Supprimer cette assignation et en faire une nouvelle" class="btn btn-primary" OnClick="window.location.href=\'reponseTxtTrousPosition.php?idQuestion='.$idQuest.'&del=true \' ">';
												}
                                            
		   ?>
                     
              </div> <!-- /.table-responsive -->

            </div> <!-- /.portlet-content -->

          </div> <!-- /.portlet -->

        </div> <!-- /.col -->

      </div> <!-- /.row -->

    </div> <!-- /.content-container -->
      
  </div> <!-- /.content -->

</div> <!-- /.container -->


<?php include('includes/footer.php'); ?>

  <script src="./js/libs/jquery-1.10.1.min.js"></script>
  <script src="./js/libs/jquery-ui-1.9.2.custom.min.js"></script>
  <script src="./js/libs/bootstrap.min.js"></script>

  <!--[if lt IE 9]>
  <script src="./js/libs/excanvas.compiled.js"></script>
  <![endif]-->
  
  <!-- Plugin JS -->
  <script src="./js/plugins/datatables/jquery.dataTables.min.js"></script>
  <script src="./js/plugins/datatables/DT_bootstrap.js"></script>
  <script src="./js/plugins/tableCheckable/jquery.tableCheckable.js"></script>
  <script src="./js/plugins/icheck/jquery.icheck.min.js"></script>
  <script src="js/plugins/magnific/jquery.magnific-popup.min.js"></script>

  <!-- App JS -->
  <script src="./js/target-admin.js"></script>
  
<div id="ModifReponse" class="modal modal-styled fade">
  <div class="modal-dialog">
    <div class="modal-content">
    <form action="reponseTxtTrous.php?idQuestion=<?php echo $idQuest; ?>" id="mformreponse" method="post" class="form parsley-form" enctype="multipart/form-data">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h3 class="modal-title">Modification de l'exercice</h3>
      </div>
      <div class="modal-body">
      	<div class="row">
              <div class="col-md-8">
              <label for="ReponseUpdate">Contenu</label><input type="text" name="ReponseUpdate" />
              </div>
            <div class="col-md-4">
            <label for="CorrecteUpdate">Correcte</label><select name="CorrecteUpdate" class="form-control">
                    <option value="0">INTRUT</option>
                    <option value="1">PAS INTRUT</option>
                  </select>
            </div>
        </div>
        <div class="row">
        	<div class="col-md-12"><input type="file" name="upload"/></div>
        </div>
      </div>
      <div class="modal-footer">
      	<input type="text" name="idRepUpdate" hidden/>
        <button type="button" class="btn btn-tertiary" data-dismiss="modal">Fermer</button>
        <input type="submit" class="btn btn-primary" name="mformreponse" value="Sauvegarder"/>
      </div>
      </form>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<script type="text/javascript">
$(document).ready(function() {
	$("a[id^='reponse']").click(function() {
		
		var id = $(this).attr("id").split("reponse");

		var rep2 = '#reponse'+id[1]+' td+td';
		var rep5 = '#reponse'+id[1]+' td+td+td+td';
		
		$("#mformreponse input[name=idRepUpdate]").val(id[1]);		
				
		$("#mformreponse input[name=ReponseUpdate]").val($(rep2).html());
		
		if($(rep5).html() == 'JUSTE'){ var rep = 1; }else{ var rep = 0;}
		$("#mformreponse select[name=CorrecteUpdate]").val(rep);
	});
});
</script>

  
</body>
</html>